package site.wanjiahao.union;

/**
 * Quick Union
 * find O(logn)
 * Union O(logn)
 *
 * 路径减半
 */
public class UnionFind_QU_R_PH extends UnionFind_QU_R {


    public UnionFind_QU_R_PH(int capacity) {
        super(capacity);
    }

    // 路径减半，使路径上的每隔一个节点，就指向其祖父节点
    @Override
    public int find(int v) {
        checkRange(v);
        while (v != parents[v]) {
            parents[v] = parents[parents[v]];
            v = parents[v];
        }
        return v;
    }
}
